package com.lagou.dao;

import com.lagou.bean.User;
import com.lagou.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImp implements UserDao{
    /**
     * 自定义抽象方法完成用户校验工作
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        //2.编写sql
        String sql = "select * from t_user where userName=? and password =?";
        //3.设置占位符参数
        Object params[] = {user.getUserName(),user.getPassword()};
        User user1 = null;
        //4.执行查询操作
        try {
            user1 = qr.query(sql, new BeanHandler<>(User.class), params);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user1;
    }
}
